from django.shortcuts import get_object_or_404
from django.utils.deprecation import MiddlewareMixin
from apps.inform.models import Inform
from apps.inform.models import InformRead

class InformMiddleware(MiddlewareMixin):
    def process_request(self,request):
        path = request.path
        informId = path.split('/')[-1]
        if '/api/inform/' in path and informId.isdecimal():
            # 返回一个对象  first 和get方法都可以获得对象
            # inform = Inform.objects.filter(pk=informId).first()
            # 返回一个queryset
            # inform = Inform.objects.filter(pk=informId).all()
            inform = get_object_or_404(Inform,pk=informId)
            inform.clicks += 1
            inform.save()

